package org.appfuse.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.GzxxDAO;
import org.appfuse.model.Gzxx;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Gzxx.
 * @see org.appfuse.model.Gzxx
 * @author MyEclipse - Hibernate Tools
 */
public class GzxxDAOHibernate  extends BaseDaoHibernate implements GzxxDAO {

    private static final Log log = LogFactory.getLog(GzxxDAOHibernate.class);

    
    public void save(Gzxx transientInstance) {
        log.debug("saving Gzxx instance");
        try {
        	getSession().setFlushMode(FlushMode.AUTO);
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
    
    public void update(Gzxx transientInstance) {
        log.debug("saving Gzxx instance");
        try {
        	getSession().setFlushMode(FlushMode.AUTO);
            getSession().update(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Gzxx persistentInstance) {
        log.debug("deleting Gzxx instance");
        try {
        	getSession().setFlushMode(FlushMode.AUTO);
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Gzxx findById( String a00, Integer id) {
        log.debug("getting Gzxx instance with id: " + id);
        try {
        	String queryString = "from Gzxx as model where model.basicInfo.id"
				 + "= ? " + " and model.id = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, a00);
			queryObject.setParameter(1, id);
			return (Gzxx) queryObject.uniqueResult();
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(Gzxx instance) {
        log.debug("finding Gzxx instance by example");
        try {
            List results = getSession()
                    .createCriteria("org.appfuse.model.Gzxx")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Gzxx instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Gzxx as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

    public Gzxx merge(Gzxx detachedInstance) {
        log.debug("merging Gzxx instance");
        try {
            Gzxx result = (Gzxx) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Gzxx instance) {
        log.debug("attaching dirty Gzxx instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Gzxx instance) {
        log.debug("attaching clean Gzxx instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    

	public Integer newId(String a00){
		log.debug("generate a new id");
		try {
			String queryString = "select max(id) from Gzxx as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		Integer maxId = (Integer) queryObject.uniqueResult();
		if (maxId != null){
			maxId = maxId + 1;
		}else{
			maxId = 1;
		}
		log.debug("generate new id successful");
		return maxId;
			
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}

	public List findByA00(String a00) {
		log.debug("generate a new id");
		try {
			String queryString = " from Gzxx as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		return queryObject.list();
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}
    
}